package com.ruoyi.io;

import java.io.*;
public class FileInputStreamExample {
    public static void main(String[] args) {
        // 要读取的文件路径和名称
        String filePath = "E:\\data.txt";
        // 创建输入流对象
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(filePath);
            byte[] buffer = new byte[1024];
            int len;
            // 使用 while 循环读取文件，每次最多读取 1024 个字节
            while ((len = fis.read(buffer)) != -1) {
                // 将读取的字节转换为字符串，并输出到控制台
                String content = new String(buffer, 0, len, "UTF-8");
                System.out.println(content);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        } finally {
            // 关闭输入流
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing file: " + e.getMessage());
            }
        }
    }
}
